#!/usr/bin/env python3
"""
Create a demo superuser for testing purposes.
"""
import asyncio
import sys
from pathlib import Path

# Add the project root to the Python path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from app.core.database import AsyncSessionLocal
from app.services.user_service import UserService
from app.schemas.user import UserCreate


async def create_demo_user():
    """Create a demo superuser."""
    print("Creating demo superuser...")
    
    # Demo user data
    user_data = UserCreate(
        email="admin@example.com",
        username="admin",
        full_name="Admin User",
        password="admin123"
    )
    
    try:
        async with AsyncSessionLocal() as db:
            # Check if user already exists
            existing_user = await UserService.get_user_by_email(db, user_data.email)
            if existing_user:
                print(f"User with email {user_data.email} already exists!")
                return
            
            existing_user = await UserService.get_user_by_username(db, user_data.username)
            if existing_user:
                print(f"User with username {user_data.username} already exists!")
                return
            
            # Create the superuser
            user = await UserService.create_user(db, user_data)
            
            # Set as superuser
            user.is_superuser = True
            await db.commit()
            await db.refresh(user)
            
            print(f"Demo superuser created successfully!")
            print(f"Email: {user.email}")
            print(f"Username: {user.username}")
            print(f"Password: admin123")
            
    except Exception as e:
        print(f"Error creating demo user: {e}")
        raise


if __name__ == "__main__":
    asyncio.run(create_demo_user())